package
{
	//include "../../MapEditor/src/Map.as";
	public class StateExplore extends State
	{
		protected var map:Map;
		protected var mapID:uint;
		protected var objects:Array = new Array();
		protected var rowCount:uint;
		protected var columnCount:uint;
		protected var offsetX:uint;
		protected var offsetY:uint;
		public function StateExplore(mapID:uint)
		{
			super();
			this.mapID = mapID;
			rowCount = FBGame.mainFBGame.height/Map.cellSize;
			if(FBGame.mainFBGame.height%Map.cellSize != 0)
			{
				rowCount++;
			}
			columnCount = FBGame.mainFBGame.width/Map.cellSize;
			if(FBGame.mainFBGame.width%Map.cellSize != 0)
			{
				columnCount++;
			}
			CommonResource.myPlayer.cellCurrent.Row = 9;
			CommonResource.myPlayer.cellCurrent.Column = 6;
		}
		public override function Init():void
		{
			var s:Vector.<ExternalResource> = new Vector.<ExternalResource>();
			s.push(new ExternalResource(CommonResource.host + "map/"+mapID.toString()+".map"
				,ExternalResource.byteArray));
			FBGame.stateStack.Push(new StateLoading(s,objects,this));
		}
		public override function LoadComplete(success:Boolean):void
		{
			if(success)
			{
				map = new Map();
				map.SetValueFromByteArray(objects[0]);
			}
			else
			{
				
			}
		}
		public override function MouseDown(x:int, y:int):void
		{
			var row:int = y/Map.cellSize + offsetY;
			var column:int = x/Map.cellSize + offsetX;
			if(map.cells[row][column] == 0)
			{
				
				return;
			}
			if(CommonResource.myPlayer.cellList == null)
			{
				CommonResource.myPlayer.cellList = map.Calculate(CommonResource.myPlayer.cellCurrent
					, new CellPos(row,column));
			}
		}
		public override function Render(g:MyGraphics):void
		{
			g.Clear(0xFFFF0000);
			for(var i:uint = 0;i<Math.min(rowCount,map.rowCount);i++)
			{
				for(var j:uint = 0;j<Math.min(columnCount,map.columnCount);j++)
				{
					if(map.cells[i+offsetY][j+offsetX] == 1)
					{
						g.FillRect(Map.cellSize*j ,Map.cellSize*i ,Map.cellSize,Map.cellSize,0xFFFFFFFF);
					}
					else
					{
						g.FillRect(Map.cellSize*j ,Map.cellSize*i ,Map.cellSize,Map.cellSize,0xFF000000);
					}
				}
			}
			g.DrawCircle((CommonResource.myPlayer.cellCurrent.Column - offsetX)*Map.cellSize + Map.cellSize/2
				,(CommonResource.myPlayer.cellCurrent.Row - offsetY)*Map.cellSize + Map.cellSize/2,10,0xFFFF00FF);
		}
		public override function Update(dt:int):void
		{
			if(map != null)
			{
				if(CommonResource.myPlayer.cellList != null)
				{
					CommonResource.myPlayer.cellCurrent = CommonResource.myPlayer.cellList.pop();
					if(CommonResource.myPlayer.cellList.length == 0)
					{
						CommonResource.myPlayer.cellList = null;
					}
					offsetX = Math.max(0,Math.min(CommonResource.myPlayer.cellCurrent.Column,map.columnCount - columnCount/2) - columnCount/2);
					offsetY = Math.max(0,Math.min(CommonResource.myPlayer.cellCurrent.Row,map.rowCount - rowCount/2) - rowCount/2);
				}
			}
		}
	}
}